<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\ux\UtilPHPExcel;

/**
 * 道具及邮件列表
 */
class PrizeMail extends Base
{

    /**
     * 控制器初始化
     * @return   void
     */
    public function _initialize()
    {
        //基类授权
        //
        parent::_initialize();
        $this->model = new \app\index\model\Mail();
    }

    /**
     * 清空表，复位自增主键列
     *
     * @return   void
     */
    public function clearTbl()
    {
        $this->model->clearTbl();

        $output['success'] = true;
        $output['msg']     = '完成清空';

        _callback($output);
    }

    public function import()
    {
        $fullname = input('file');
        $title    = array("id", "type", "title", "content", "prize");
        $res      = $this->importToTbl($fullname, $title);

        if ($res) {
            $output['data']    = $res;
            $output['success'] = true;
            $output['msg']     = 'success';
            _callback($output);
        } else {

            $output['success'] = false;
            $output['msg']     = 'false';
            _callback($output);
        }
    }

    /**
     * Excel文件转数组
     * @DateTime 2018-01-26
     * @param    [type] $fullname [description]
     * @return   [type]
     */
    public function getExcelArray($fullname, $title)
    {
        return (UtilPHPExcel::excelToArray($fullname, $title));
    }

    /**
     * Excel文件转数组
     * @DateTime 2018-01-26
     * @param    [type] $fullname [description]
     * @return   [type]
     */
    public function importToTbl($fullname, $title)
    {
        try {

            $rows = UtilPHPExcel::excelToArray($fullname, $title);
            $res  = $this->model->allowField(true)->saveAll($rows);
            return $res;
        } catch (\Exception $e) {
            throw new \think\Exception($e->getMessage());
        }

    }
}
